let that,
istest = 0,

util = new (require('../../utils/util.js')),
api = new (require('../../utils/api.js'))
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stateword:'未开通',
    state:'',
    userinfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    that = this
    let superinfo = wx.getStorageSync('superinfo'),
   userinfo = wx.getStorageSync('userInfo'),
    {stateword}= this.data
    console.log(superinfo,'superinfo');
    // superinfo.isSuper=0
    // superinfo.expired=1
    if(istest==1){
      userinfo.phone=''
      userinfo.litPic=''
      userinfo.name=''
      superinfo.isSuper=0
    }

    if(superinfo.isSuper==1){
      stateword = `有效期：${util.formatDate(new Date(superinfo.startTime*1000),'-')}至${util.formatDate(new Date(superinfo.endTime*1000),'-')}`
    }
    this.setData({superinfo,userinfo,phone:userinfo.phone,stateword})
    // if(istest){
    //   superinfo.isSuper=0
    //   userinfo.phone=''
    // }
    if(superinfo.isSuper==1){
      stateword = `有效期：${util.formatDate(new Date(superinfo.startTime*1000),'-')}至${util.formatDate(new Date(superinfo.endTime*1000),'-')}`
    }
    this.setData({superinfo,stateword,userinfo})
  },
  kaitong(e){
    let{ischeck,superinfo,userinfo} =this.data,{gm} = e.currentTarget.dataset
    console.log({ischeck,gm});
    if(!ischeck&&gm!=1){
      wx.showModal({
        title: '提示',
        content: '请先勾选协议',
        showCancel:!1,
        complete: (res) => {
        }
      })
    }else{
      api.超级会员下单操作(superinfo.chargeId,res=>{
        if(res.data.code==0){
          api.超会订单微信发起订单支付(res.data.entity.data.orderId,res=>{
              console.log(res,'res超会订单微信发起订单支付');
              if(res.succ==1){
                let info = {isSuper:1,expired:0}
                that.setData({superinfo:info})
                api.获取超级会员信息(res=>{
                  if(res.data.code==0){
                    let superinfo = res.data.entity.data
                    that.setData({isSuper:superinfo.isSuper==1})
                   wx.setStorageSync('superinfo',superinfo )
                    stateword = `有效期：${util.formatDate(new Date(superinfo.startTime*1000),'-')}至${util.formatDate(new Date(superinfo.endTime*1000),'-')}`
                  that.setData({superinfo,stateword})
                  } 
                 })
              }
          })
        }
      })
    }
  },

   // 授权手机号
   getPhoneNumber(e) {
    let { userinfo } = that.data
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      api.wxphone({ encryptedData: e.detail.encryptedData, iv: e.detail.iv }, res => {
        if (res.data.code == 0) {
          let { phoneNumber } = res.data.entity.data
          userinfo.phone = phoneNumber
          wx.setStorageSync('userInfo', userinfo)
          that.setData({ phone:phoneNumber })
          wx.showToast({
            title: '授权成功',
            icon: 'none',
            duration: 1000
          })
          
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      })
    }
  },

  gouse(){
    wx.switchTab({
      url: '../supermember/supermember',
    })
  },
  cb(){
    this.setData({ischeck:!this.data.ischeck})
  },

  toxieyi(){
    wx.navigateTo({
      url: '../hyxy/hyxy',
    })
  },

   // 授权微信登录
   getUserProfile(e) {
   // let { userinfo } = that.data
    wx.getUserProfile({
      desc: '用于完善用户资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        let userinfo = res.userInfo
        console.log(res.userInfo, 'resres');
        if (res.userInfo) {
          api.updatewxinfo({ nickName: userinfo.nickName, avatarUrl: userinfo.avatarUrl }, res => {
            console.log(res, 'resresresres');
            // let getuserInfo=wx.getStorageSync('userInfo')
            userinfo.litPic = userinfo.avatarUrl
            userinfo.name = userinfo.nickName
           // console.log(data, 'dataaaaaaaa');
            that.setData({ userinfo })
            wx.setStorageSync('userInfo', userinfo)
            if (res.data.code == 0) {
              wx.showToast({
                title: '授权成功',
                icon: 'none',
                duration: 1000
              })
            }
          })
        } else {
          console.log("点击了拒绝授权");
        }
      },
      fail(res) {
        wx.showToast({
          title: '拒绝授权将无法进入小程序，请重新点击授权',
          icon: 'none'
        })
        console.log(res, 'ressss');
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})